//
//  GatherPickerCell.m
//  YYTextTest
//
//  Created by macintosh on 16/9/12.
//  Copyright © 2016年 macintosh. All rights reserved.
//

#import "GatherPickerCell.h"

@interface GatherPickerCell () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) UIPickerView *pickerView;
@end

@implementation GatherPickerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        if (!_pickerView) {
            _pickerView = [[UIPickerView alloc] init];
            _pickerView.delegate = self;
            _pickerView.dataSource = self;
            [self.contentView addSubview:_pickerView];
            self.clipsToBounds = YES;
        }
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _pickerView.center = CGPointMake(CGRectGetMidX(self.contentView.bounds), CGRectGetMidY(self.contentView.bounds));
}

- (void)setPickerDatas:(NSArray *)pickerDatas
{
    _pickerDatas = pickerDatas;
    [_pickerView reloadAllComponents];
}

- (void)setSelectedRow:(NSInteger)selectedRow
{
    _selectedRow = selectedRow;
    [_pickerView selectRow:_selectedRow inComponent:0 animated:NO];
}



#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerDatas.count;
}

#pragma mark - UIPickerViewDelegate

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return _pickerView.bounds.size.width;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 25;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%@", _pickerDatas[row]];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (_pickerSelected) {
        _pickerSelected(row);
    }
}

@end
